﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using VacationDaysCommons;
using VacationDaysDatabase;

namespace VacationDaysClient
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        

        private void button1_Click(object sender, RoutedEventArgs e)
        {

            using (VDUserServiceClient vc = new VDUserServiceClient())
            {
                var userInfo = vc.LoginUser(userNameBox.Text, passwordBox.Password);

                if (userInfo == null)
                {
                    MessageBox.Show("The enetered credentials are invalid.\nPlease try again!");
                    userNameBox.Text = "";
                    passwordBox.Password = "";
                    userNameBox.Focus();
                }
                else
                {
                    switch (userInfo.Role){
                        case EmployeeRole.Admin:
                            Application.Current.MainWindow = new AdminWindow();
                        break;
                        case EmployeeRole.Employee:
                        case EmployeeRole.HRPeople:
                        case EmployeeRole.PM:
                            Application.Current.MainWindow = new UserWindow(userInfo);//AdminMainWindow(userInfo);
                        break;

                    }
                    Application.Current.MainWindow.Show();
                    Close();
                }
                vc.Close();
            }
         }

        private void Grid_Loaded(object sender, RoutedEventArgs e)
        {
            userNameBox.Focus();
        }

        private void passwordBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                button1_Click(sender, new RoutedEventArgs());
            }
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            Application.Current.MainWindow = new RegisterUser();
            Application.Current.MainWindow.Show();
            Close();
        }
        }
}
